package net.thxjj.adminServer.service.impl;

import com.alibaba.fastjson.JSON;
import net.thxjj.adminServer.entity.*;
import net.thxjj.adminServer.entity.aep.*;
import net.thxjj.adminServer.mongodao.*;
import net.thxjj.adminServer.service.IDeviceJsonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceJsonService implements IDeviceJsonService {
    @Autowired
    private MongoProfileDao mongoProfileDao;
    @Autowired
    private MongoLoginDao mongoLoginDao;
    @Autowired
    private MongoSmokeDao mongoSmokeDao;
    @Autowired
    private MongoHeartDao mongoHeartDao;
    @Autowired
    private MongoEventDao mongoEventDao;

    @Override
    public List<DevLog> getEventDataAtTime(String device_id, long start_time, long end_time) {
        return mongoEventDao.getDataAtTime(device_id,start_time,end_time);
    }

    @Override
    public List<StatusBean> getStatusDataAtTime(String device_id, long start_time, long end_time) {

        List<LoginData> list_logindata = mongoLoginDao.getLoginDataAtTime(device_id,start_time,end_time);

        List<HeartData> list_heartData = mongoHeartDao.getHeartBeatDataAtTime(device_id,start_time,end_time);

        List<SmokeData> list_smokedata = mongoSmokeDao.getSmokeDataAtTime(device_id,start_time,end_time);

        List<ProfileLog> list_profilelog =  mongoProfileDao.getProfileLogsByTime(device_id,start_time,end_time);

        List<StatusBean> list = new ArrayList<StatusBean>();

        for(int i=0;i<list_profilelog.size();i++){
            ProfileLog profileLog = list_profilelog.get(i);
            StatusBean statusBean = new StatusBean();
            String content = JSON.toJSONString(profileLog);
            statusBean.setEventContent(content);
            statusBean.setServiceId(4);
            statusBean.setTimestamp(profileLog.getTimestamp());
            list.add(statusBean);
        }


        for(int i=0;i<list_logindata.size();i++){
            LoginData loginData = list_logindata.get(i);
            StatusBean statusBean = new StatusBean();
            String content = JSON.toJSONString(loginData);
            statusBean.setEventContent(content);
            statusBean.setServiceId(3);
            statusBean.setTimestamp(loginData.getTimestamp());
            list.add(statusBean);
        }

        for(int i=0;i<list_heartData.size();i++){
            HeartData heartData = list_heartData.get(i);
            StatusBean statusBean = new StatusBean();
            String content = JSON.toJSONString(heartData);
            statusBean.setEventContent(content);
            statusBean.setServiceId(2);
            statusBean.setTimestamp(heartData.getTimestamp());
            list.add(statusBean);
        }

        for(int i=0;i<list_smokedata.size();i++){
            SmokeData smokeData = list_smokedata.get(i);
            StatusBean statusBean = new StatusBean();
            String content = JSON.toJSONString(smokeData);
            statusBean.setEventContent(content);
            statusBean.setServiceId(1);
            statusBean.setTimestamp(smokeData.getTimestamp());
            list.add(statusBean);
        }
        sortList(list);

        return list;
    }

    private void sortList(List<StatusBean> list) {
        if (list.size() <= 1) {

        } else {
            for (int i = 0; i < list.size() - 1; i++) {
                for (int j = 0; j < list.size() - 1 - i; j++) {
                    if (list.get(j).getTimestamp() < list.get(j+1).getTimestamp()) {
                        StatusBean statusBean1 = new StatusBean();
                        statusBean1.setTimestamp(list.get(j).getTimestamp());
                        statusBean1.setServiceId(list.get(j).getServiceId());
                        statusBean1.setEventContent(list.get(j).getEventContent());

                        StatusBean statusBean2 = new StatusBean();
                        statusBean2.setTimestamp(list.get(j+1).getTimestamp());
                        statusBean2.setServiceId(list.get(j+1).getServiceId());
                        statusBean2.setEventContent(list.get(j+1).getEventContent());

                        list.set(j,statusBean2);
                        list.set(j+1,statusBean1);
                    }
                }
            }

        }
    }
}
